package com.nf.demo.dao.impl;

import com.nf.demo.dao.StaffsDAO;
import com.nf.demo.model.pojo.Staffs;
import com.nf.demo.utils.DBUtils;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @author lwj
 * @date 2025-02-24
 * @vesion 1.0.0
 */
public class StaffsDAOImpl implements StaffsDAO {
    @Override
    public ArrayList<Staffs> selectAll() {
        ArrayList<Staffs> staffs = new ArrayList<Staffs>();
        ArrayList<Object> params = new ArrayList<Object>();
        // 开启连接
        Connection connection = DBUtils.getConnection();
        String sql = "select id,name,age,phone,sta_pos staPos,add_time addTime,update_time updateTime from staffs";
        ResultSet resultSet = DBUtils.executeQuery(sql,params);
        try{
            while(resultSet.next()){
                Staffs staff = new Staffs();
                staff.setId(resultSet.getInt(1));
                staff.setName(resultSet.getString(2));
                staff.setAge(resultSet.getInt(3));
                staff.setPhone(resultSet.getString(4));
                staff.setStaPos(resultSet.getString(5));
                staff.setAddTime(resultSet.getDate(6));
                staff.setUpdateTime(resultSet.getDate(7));

                // 添加进数组
                staffs.add(staff);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            // 关闭资源
            DBUtils.closeConnection(connection,null,resultSet);
        }
        return staffs;
    }

    @Override
    public Integer delete(Staffs staffs) {
        Integer count = -1;
        ArrayList<Object> params = new ArrayList<Object>();
        // 开启连接
        Connection connection = DBUtils.getConnection();
        String sql = "delete from staffs where id = ?";
        params.add(staffs.getId());
        count = DBUtils.executeUpdate(sql,params);
        // 关闭资源
        DBUtils.closeConnection(connection,null,null);
        return count;
    }

    @Override
    public Integer update(Staffs staffs) {
        Integer count = -1;
        ArrayList<Object> params = new ArrayList<Object>();
        // 开启连接
        Connection connection = DBUtils.getConnection();
        String sql = "update staffs set name = ?, age = ?, phone = ?, sta_pos = ?,update_time = ? where id = ?";
        params.add(staffs.getName());
        params.add(staffs.getAge());
        params.add(staffs.getPhone());
        params.add(staffs.getStaPos());
        params.add(LocalDateTime.now());
        params.add(staffs.getId());
        count = DBUtils.executeUpdate(sql,params);
        // 关闭资源
        DBUtils.closeConnection(connection,null,null);
        return count;
    }

    @Override
    public Integer insert(Staffs staffs) {
        Integer count = -1;
        ArrayList<Object> params = new ArrayList<Object>();
        // 开启连接
        Connection connection = DBUtils.getConnection();
        String sql = "insert into staffs(name,age,phone,sta_pos) values (?,?,?,?);";
        params.add(staffs.getName());
        params.add(staffs.getAge());
        params.add(staffs.getPhone());
        params.add(staffs.getStaPos());
        count = DBUtils.executeQueryAddId(sql,params);
        // 关闭资源
        DBUtils.closeConnection(connection,null,null);
        return count;
    }

    @Override
    public List<Staffs> selectPage(int page, int totalRow) {
        return null;
    }

    @Override
    public Staffs selectById(int id) {
        return null;
    }
}
